<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace  app\api\controller;

use  Mscore\Core\Controller;
use  app\api\model\Web;
use  app\api\model\Common as CommonModel;
use  Mscore\Core\Util\Util;
use  Mscore\Core\Lang;
use  Mscore\Core\Lib\ArrayMethod;

class WebController extends Controller{

    private $model;
    public $success;
    public $error;
    public $header;

    public function __construct()  
    {    
        $this->model = new Web();
        
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])&&!empty($this->header['language'])?$this->header['language']:config('langs.default');
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
  
    }

    /**
     * 获取config
     * @param array $getconfig 
    */
    public function getconfig(){
          $data = $this->model->getconfig('id=1');
          $this->success['data']=$data;
          return $this->echojson($this->success);
    }

    /**
     * 获取getLang
     * @param array $getLang 
    */
    public function getLang(){
          $data = $this->model->getLang();
          $this->success['data']=$data;
          return $this->echojson($this->success);
    }

    /**
     * 获取菜单都由後端控制
     * @param array $menul 
    */
    public function menu(){
        $where = 'status>0 and special<1'; 
        $data = $this->model->menuList($where,'id,title,titles,acode,pagecode,param,url,pid');
        foreach ($data as $key => $value) {
            if(!empty($value['titles'])){
                $data[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
            }
            if(!empty($value['param'])){
                $data[$key]['param']=json_decode($value['param'],true);  
            }
            unset($data[$key]['titles']);
        }
        $data = ArrayMethod::dealListToTree($data,0,'id','pid','mue',false);
        $this->success['data']=$data;
        return $this->echojson($this->success);
    }

    /**
     * 获取菜单
     * @param array $menul  前端控制一級菜單
    */
    public function menul(){
        $where = 'status>0 and special<1'; 
        if(post('acode')){
            $where .= ' and acode= "'.post('acode').'"';
        }
        $data = $this->model->menuList($where,'id,title,titles,acode,pagecode,param,url');
        foreach ($data as $key => $value) {
            if(!empty($value['titles'])){
                $data[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
            }
            if(!empty($value['param'])){
                $data[$key]['param']=json_decode($value['param'],true);  
            }
            unset($data[$key]['titles']);
        }
        $this->success['data']=$data;
        return $this->echojson($this->success);
    }

    /**
     * 获取菜单,
     * @param array $menur 
    */
    public function menur(){
        $where = 'status>0 and special>0'; 
        if(post('acode')){
            $where .= ' and acode= "'.post('acode').'"';
        }
        $data = $this->model->menuList($where,'id,title,titles,acode,pagecode,param,url');
        foreach ($data as $key => $value) {
            if(!empty($value['titles'])){
                $data[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
            }
            if(!empty($value['param'])){
                $data[$key]['param']=json_decode($value['param'],true);  
            }
            unset($data[$key]['titles']);
        }
        $this->success['data']=$data;
        return $this->echojson($this->success);
    }

    /**
     * 获取客服
     * @param array $getcuster 
    */
    public function getcuster(){
          $data = $this->model->getcuster('type=3',$this->header['language']);
          $this->success['data']=$data;
          return $this->echojson($this->success);
    }

    /**
     * 产品类别列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function catelist(){
        $cate=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'a.id='.post('id');
                $cate = $this->model->catelist($where);
                if($cate['data'][0]['imgurl']){
                    $cate['data'][0]['imgurl']=config('file.baseurl').$cate['data'][0]['imgurl'];
                }
                if(!empty($cate['data'][0]['titles'])){
                    $cate['data'][0]['title']=isset(json_decode($cate['data'][0]['titles'],true)[$this->header['language']])?json_decode($cate['data'][0]['titles'],true)[$this->header['language']]:$cate['data'][0]['title'];
                    $cate['data'][0]['price']=0;
                    unset($cate['data'][0]['titles']);
                }
                $this->success['data']=$cate['data'];
                $this->success['count']=$cate['total'];
                return $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = 'a.dels<1';
                $cate = $this->model->catelist($where,$page,$limit,['id','title','pid','orderid','acode','pagecode']);
                foreach ($cate['data'] as $key => $value) {
                    if(!empty($cate['titles'])){
                        $cate['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
                    }
                    unset($cate['data'][$key]['titles']);
                }
                $this->success['data']=$cate['data'];
                $this->success['count']=$cate['total'];
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 产品列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function trade(){
        $pro=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'a.id='.post('id');
                $pro = $this->model->trade($where);
                if(!empty($pro['data'][0]['titles'])){
                    $pro['data'][0]['title']=isset(json_decode($pro['data'][0]['titles'],true)[$this->header['language']])?json_decode($pro['data'][0]['titles'],true)[$this->header['language']]:$pro['data'][0]['title'];
                    $pro['data'][0]['price']=0;
                    unset($pro['data'][0]['titles']);
                }
                $this->success['data']=$pro['data'];
                $this->success['count']=$pro['total'];
                return $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = 'a.dels<1 and a.status<3';
                if(!empty(post('cid'))){
                    $where .= ' and a.cid='.post('cid');
                }
                $pro = $this->model->trade($where,$page,$limit,['id','title','titles','cid','procode']);
                $procode='';
                foreach ($pro['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $pro['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
                    }
                    $pro['data'][$key]['price']=0;
                    $procode .= $value['procode'].',';
                    unset($pro['data'][$key]['titles']);
                }
                $this->success['data']=$pro['data'];
                $this->success['procode']=$procode;
                $this->success['count']=$pro['total'];
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 文章类别列表
     * @param array $arcate 
     * @param array $where 
     */ 
    public function arcate(){
        $cate=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'id='.post('id');
                $cate = $this->model->getcatelist($where);
                if($cate[0]){
                    $cate[0]['imgurl']=config('file.baseurl').$cate['data'][0]['imgurl'];
                }
                $this->success['data']=$cate;
                return $this->echojson($this->success);
                break;
            default:
                $where = ' status > 0 ';
                if(post('acode')){
                    $where .= ' and acode="'.post('acode').'"';
                }
                if(post('pagecode')){
                    $where .= ' and pagecode="'.post('pagecode').'"';
                }
                if(post('pid')){
                    $where .= ' and pid='.post('pid');
                }
                $cate = $this->model->getcatelist($where,['id','title','introduction','titles','acode','pagecode']);
                foreach ($cate as $key => $value) {
                    if(!empty($value['introduction'])){
                        $cate[$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['titles'])){
                        $cate[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                }
                $this->success['data']=$cate;
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 文章类别列表
     * @param array $arcate 
     * @param array $where 
     */ 
    public function arcateinfo(){
        $where = ' status > 0 ';
        if(post('acode')){
            $where .= ' and acode="'.post('acode').'"';
        }
        if(post('pagecode')){
            $where .= ' and pagecode="'.post('pagecode').'"';
        }
        if(post('pid')=='0' || !empty(post('pid'))){
            $where .= ' and pid='.post('pid');
        }
        $cate = $this->model->getcateinfo($where,['id','title','introduction','titles','acode','pagecode','imgurl']);
        if($cate){
            $cate['imgurl']=config('file.baseurl').$cate['imgurl'];
            if(!empty($cate['introduction'])){
                $cate['introduction']=isset(json_decode($cate['introduction'],true)[$this->header['language']])?json_decode($cate['introduction'],true)[$this->header['language']]:'';  
            }
            if(!empty($cate['titles'])){
                $cate['title']=isset(json_decode($cate['titles'],true)[$this->header['language']])?json_decode($cate['titles'],true)[$this->header['language']]:$cate['title'];
            }
        }
        $this->success['data']=$cate;
        return $this->echojson($this->success);
    }

    /**
     * 新闻列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function article(){
        $article=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'id='.post('id');
                $article = $this->model->arlist($where);
                if(!empty($article['data'][0]['titles'])){
                    $article['data'][0]['title']=isset(json_decode($article['data'][0]['titles'],true)[$this->header['language']])?json_decode($article['data'][0]['titles'],true)[$this->header['language']]:$article['data'][0]['title'];
                    unset($article['data'][0]['titles']);
                }
                if(!empty($article['data'][0]['cname'])){
                    $article['data'][0]['cname']=isset(json_decode($article['data'][0]['cname'],true)[$this->header['language']])?json_decode($article['data'][0]['cname'],true)[$this->header['language']]:$article['data'][0]['cname'];
                }
                if(!empty($article['data'][0]['introduction'])){
                    $article['data'][0]['introduction']=isset(json_decode($article['data'][0]['introduction'],true)[$this->header['language']])?json_decode($article['data'][0]['introduction'],true)[$this->header['language']]:$article['data'][0]['introduction'];
                }
                if(!empty($article['data'][0]['content'])){
                    $article['data'][0]['content']=isset(json_decode($article['data'][0]['content'],true)[$this->header['language']])?json_decode($article['data'][0]['content'],true)[$this->header['language']]:$article['data'][0]['content'];
                    $article['data'][0]['content'] = htmlspecialchars_decode($article['data'][0]['content']);
                }
                if(!empty($article['data'][0]['createtime'])){
                    $article['data'][0]['createtime']=date('Y-m-d',strtotime($article['data'][0]['createtime']));
                }
                
                $this->success['data']=$article['data'];
                $this->success['count']=$article['total'];
                return $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = 'dels<1 and status<3';
                if(!empty(post('cid'))){
                    $where .= ' and cid='.post('cid');
                }
                $article = $this->model->arlist($where,$page,$limit,['id','cname','title','titles','imgurl','introduction','createtime','open_url','is_open']);
                foreach ($article['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $article['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
                    }
                    if(!empty($value['introduction'])){
                        $article['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:$value['title'];  
                    }
                    if(!empty($value['cname'])){
                        $article['data'][$key]['cname']=isset(json_decode($value['cname'],true)[$this->header['language']])?json_decode($value['cname'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['imgurl'])){
                        $article['data'][$key]['imgurl']=config('file.baseurl').$article['data'][$key]['imgurl'];  
                    }
                    if($value['is_open']>0){
                        $article['data'][$key]['url']=$value['open_url'];
                    }
                    unset($article['data'][$key]['titles']);
                }
                $this->success['data']=$article['data'];
                $this->success['count']=$article['total'];
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 新闻列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function indexarticle(){
        $article=[];
        $page = post('page',1);
                $limit = post('limit',5);
                $where = 'dels<1 and status<3 and is_index>1';
                $article = $this->model->indexarticle($where,$page,$limit,['id','cname','title','titles','imgurl','introduction','createtime','open_url','is_open']);
                foreach ($article as $key => $value) {
                    if(!empty($value['titles'])){
                        $article[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];  
                    }
                    if(!empty($value['introduction'])){
                        $article[$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:$value['title'];  
                    }
                    if(!empty($value['cname'])){
                        $article[$key]['cname']=isset(json_decode($value['cname'],true)[$this->header['language']])?json_decode($value['cname'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['imgurl'])){
                        $article[$key]['imgurl']=config('file.baseurl').$article[$key]['imgurl'];  
                    }
                    unset($article[$key]['titles']);
                }
                $this->success['data']=$article;
                return $this->echojson($this->success); 
    }

    /**
     * 文档类别列表
     * @param array $concate 
     * @param array $where 
     */ 
    public function concate(){
        $cate=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'id='.post('id');
                $cate = $this->model->getconcate($where);
                if(!empty($cate[0]['introduction'])){
                        $cate[0]['introduction']=isset(json_decode($cate[0]['introduction'],true)[$this->header['language']])?json_decode($cate[0]['introduction'],true)[$this->header['language']]:'';  
                }
                if(!empty($cate[0]['titles'])){
                    $cate[0]['title']=isset(json_decode($cate[0]['titles'],true)[$this->header['language']])?json_decode($cate[0]['titles'],true)[$this->header['language']]:$cate[0]['title'];
                    unset($cate[0]['titles']);
                }
                if(!empty($cate[0]['content'])){
                    $cate[0]['content']=isset(json_decode($cate[0]['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($cate[0]['content'],true)[$this->header['language']]):'';
                }
                if($cate[0]['imgurl']){
                    $cate[0]['imgurl']=config('file.baseurl').$cate[0]['imgurl'];
                }
                $this->success['data']=$cate;
                return $this->echojson($this->success);
                break;
            default:
                $where = ' status > 0 ';
                if(post('acode')){
                    $where .= ' and acode="'.post('acode').'"';
                }
                if(post('pagecode')){
                    $where .= ' and pagecode="'.post('pagecode').'"';
                }
                // $cate = $this->model->getconcate($where,['id','title','introduction','titles','acode','pagecode','type']);
                $cate = $this->model->getconcate($where);
                foreach ($cate as $key => $value) {
                    if(!empty($value['introduction'])){
                        $cate[$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['content'])){
                        $cate[$key]['content']=isset(json_decode($value['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($value['content'],true)[$this->header['language']]):'';  
                    }
                    if(!empty($value['titles'])){
                        $cate[$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                }
                $this->success['data']=$cate;
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     *  文档列表
     * @param array $contract 
     * @param array $where 
     */ 
    public function contract(){
        $article=[];
        switch (true) {
            case !empty(post('id')):
                $where = 'id='.post('id');
                $article = $this->model->concatelist($where);
                if(!empty($article['data'][0]['titles'])){
                    $article['data'][0]['title']=isset(json_decode($article['data'][0]['titles'],true)[$this->header['language']])?json_decode($article['data'][0]['titles'],true)[$this->header['language']]:$article['data'][0]['title'];
                    unset($article['data'][0]['titles']);
                }
                if(!empty($article['data'][0]['introduction'])){
                    $article['data'][0]['introduction']=isset(json_decode($article['data'][0]['introduction'],true)[$this->header['language']])?json_decode($article['data'][0]['introduction'],true)[$this->header['language']]:'';  
                }
                if(!empty($article['data'][0]['content'])){
                    $article['data'][0]['content']=isset(json_decode($article['data'][0]['content'],true)[$this->header['language']])?json_decode($article['data'][0]['content'],true)[$this->header['language']]:'';
                    $article['data'][0]['content'] = htmlspecialchars_decode($article['data'][0]['content']);  
                }
                if(!empty($article['data'][0]['cname'])){
                        $article['data'][0]['cname']=isset(json_decode($article['data'][0]['cname'],true)[$this->header['language']])?json_decode($article['data'][0]['cname'],true)[$this->header['language']]:'';  
                    }
                if(!empty($article['data'][0]['imgurl'])){
                    $article['data'][0]['imgurl']=config('file.baseurl').$article['data'][0]['imgurl'];  
                }
                $this->success['data']=$article['data'];
                $this->success['count']=$article['total'];
                return $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = 'dels<1 and status<3';
                if(!empty(post('cid'))){
                    $where .= ' and cid='.post('cid');
                }
                // $article = $this->model->concatelist($where,$page,$limit,['id','cname','title','titles','imgurl','introduction','createtime']);
                $article = $this->model->concatelist($where,$page,$limit);
                foreach ($article['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $article['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                    if(!empty($value['introduction'])){
                        $article['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['content'])){
                        $article['data'][$key]['content']=isset(json_decode($value['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($value['content'],true)[$this->header['language']]):'';  
                    }
                    if(!empty($value['cname'])){
                        $article['data'][$key]['cname']=isset(json_decode($value['cname'],true)[$this->header['language']])?json_decode($value['cname'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['imgurl'])){
                        $article['data'][$key]['imgurl']=config('file.baseurl').$article['data'][$key]['imgurl'];  
                    }
                    unset($article['data'][$key]['titles']);
                }
                $this->success['data']=$article['data'];
                $this->success['count']=$article['total'];
                return $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 产品信息
     * @param array $protrade 
     * @param array $where 
     */ 
    public function protrade(){
        $data=[];
        switch (true) {
            case !empty(post('id')):
                $where = ' a.id='.post('id');
                $data = $this->model->protrade($where);
                $this->success['data']=$data['data'];
                $this->success['count']=$data['total'];
                return $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.status > 0 ';
                if(post('cid')){
                    $where .=" and a.cid=".post('cid');
                }
                if(!empty(post('code'))){
                    $file=['id','proid','title','procode','cid','cname','status',post('code')];
                }else{
                    $file='*';
                }
                $data = $this->model->protrade($where, $page, $limit, $file);
                foreach ($data['data'] as $key => $value) {
                    if(!empty(post('code'))&&!empty($value[post('code')])){
                        $data['data'][$key][post('code')]=json_decode($value[post('code')],true);
                    }else{
                        $data['data'][$key][post('code')]=[];
                    }
                }
                $this->success['data']=$data['data'];
                $this->success['count']=$data['total'];
                return $this->echojson($this->success);
                break;
        }
    }

    /**
     * 通用规则信息
     * @param array $traderule 
     * @param array $where 
     */ 
    public function traderule(){
        $control=(new CommonModel())->getBaseinfo('keyname="tradecontrol"','content');
        $control=!empty($control)?$control['content']:config('tradecontrol');
        $this->success['data']=$control;
        return $this->echojson($this->success); 
    }

}